<template>
  <div class="container">
    <el-form :model="ruleForm" status-icon :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
        <el-form-item label="用户名" prop="usename">
            <el-input class="ps" type="usenameword" v-model="ruleForm.usename" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="密码" prop="password">
            <el-input type="usenameword"  v-model="ruleForm.password" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="验证码" prop="captcha">
            <el-input v-model.number="ruleForm.captcha" type="text"></el-input>
        </el-form-item>
        <el-form-item>
            <el-button type="primary" @click="submitForm('ruleForm')">提交</el-button>
            <el-button @click="resetForm('ruleForm')">重置</el-button>
        </el-form-item>
    </el-form>
  </div>
</template>
<script>
    //校验验证码
     var validateCaptcha = (rule, value, callback) => {
        var userNamePattern = /^[a-zA-Z0-9]{4}$/;
        if(userNamePattern.test(value)){
            callback()
        }else{
            callback("验证码错误")
        }
      };
    //校验用户名
      var validateUsername = (rule, value, callback) => {
        console.log(rule)
        console.log(value)
        //用户名正则，3到16位（字母，数字，下划线，减号）
        var userNamePattern = /^[a-zA-Z0-9_-]{3,16}$/;
        //如果校验通过，要直接回调callback（）
        //如果校验不通过，callback中传参数
        if(userNamePattern.test(value)){
            callback()
        }else{
            callback("3到16位（字母，数字，下划线，减号）")
        }
      };
    //校验密码
      var validatePassword = (rule, value, callback) => {
       var uPattern = /^[a-zA-Z0-9_-]{3,16}$/;
       if(uPattern.test(value)){
            callback()
        }else{
            callback("3到16位（字母，数字，下划线）")
        }
      };
  export default {
    data() {
 
      return {
        ruleForm: {
          usename: '',
          password: '',
          captcha: ''
        },
        rules: {
          usename: [
            { validator: validateUsername, trigger: 'blur' }
          ],
          password: [
            { validator: validatePassword, trigger: 'blur' }
          ],
          captcha: [
            { validator: validateCaptcha, trigger: 'blur' }
          ]
        }
      };
    },
    methods: {
      submitForm(formName) {
        this.$refs[formName].validate((valid) => {
          if (valid) {
            alert('submit!');
          } else {
            console.log('error submit!!');
            return false;
          }
        });
      },
      resetForm(formName) {
        this.$refs[formName].resetFields();
      }
    }
  }
</script>
<style lang="less" scoped>
 /* //修改element-ui的样式
   //1.打开调试器，找准类名，覆盖样式
   //2.看权重，使用！important
   //3.使用深度选择器  >>> /deep/ */

   .ps{
       /deep/.el-input__inner
        {
            width:300px ;
        };
   }
</style>